package com.cx.mall.goods.controller;

import com.cx.mall.common.model.goods.Stock;
import com.cx.mall.common.utils.ResponseVO;
import com.cx.mall.common.model.goods.SkuInfo;
import com.cx.mall.goods.service.ISkuInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author lyt
 * @since 2025-07-10
 */
@RestController
@RequestMapping("goods/skuInfo")
public class SkuInfoController {
    @Autowired
    private ISkuInfoService skuInfoService;

    /**
     * 按商品id查询
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseVO getById(@PathVariable Long id){
        SkuInfo skuInfo = skuInfoService.getById(id);
        return ResponseVO.success(skuInfo);
    }

    /**
     * 按类别名称和品牌名称查询
     * @param categoryName
     * @param brandName
     * @return
     */
    @GetMapping("/find")
    public ResponseVO listByCondition(String categoryName,String brandName){
        Map<String,Object> map = new HashMap<>();
        map.put("category_name",categoryName);
        map.put("brand_name",brandName);
        List<SkuInfo> skuInfos = skuInfoService.listByMap(map);
        return ResponseVO.success(skuInfos);
    }

    /**
     * 修改商品库存信息
     * @param skuInfo
     * @return
     */
    @PutMapping
    public ResponseVO update(@RequestBody SkuInfo skuInfo){
        skuInfoService.updateById(skuInfo);
        return ResponseVO.success();
    }

    @PutMapping("/decreaseStock")
    public ResponseVO decreaseStock(@RequestBody List<Stock> stocks){
        skuInfoService.decreaseStock(stocks);
        return ResponseVO.success();
    }

    @GetMapping("/queryByUserIdAndSkuId")
    public ResponseVO queryByUserIdAndSkuId(String userId,String skuId){
        String id = skuInfoService.queryByUserIdAndSkuId(userId, skuId);
        return ResponseVO.success(id);
    }

    @GetMapping( "/listAll")
    public ResponseVO listAll(){
        List<SkuInfo> skuInfos = skuInfoService.list();
        return ResponseVO.success(skuInfos);
    }



}
